<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @cancel="handleCancel"
    v-drag
  >
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="loadData(1)">
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd()" type="primary" icon="plus">{{ $t('新增') }}</a-button>
    </div>
    
    <!-- table区域-begin -->
    <div>
      <a-table
        :columns="columns"
        :dataSource="dataSource"
        size="middle"
        bordered
        class="j-table-force-nowrap"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('编辑') }}</a>
          <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)" style="margin-left: 8px">
            <a>{{ $t('删除') }}</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <template slot="footer">
      <a-row :gutter="20">
        <a-col :span="24">
          <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
        </a-col>
      </a-row>
    </template>
    <bbpay-modal ref="bbpayModalForm" @ok="modalFormOk"></bbpay-modal>
  </a-modal>
</template>

<script>
import { getAction, downFile, deleteAction } from '@api/manage'
import {initDictOptions} from "@comp/dict/JDictSelectUtil";
import BbpayModal from './BbpayModal'
let _self
export default {
  name: 'AccountFlowModal',
  components: {
    BbpayModal,
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title:this.$t('用户名'),
          align:"center",
          dataIndex: 'userName'
        },
        {
          title:this.$t('密码'),
          align:"center",
          dataIndex: 'password'
        },
        {
          title: this.$t('IP地址'),
          align: 'center',
          dataIndex: 'ip'
        },
        {
          title: this.$t('c_吧台'),
          align: 'center',
          dataIndex: 'deviceId_dictText'
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/pay/bisPayChannelBBPay/list',
        delete: '/pay/bisPayChannelBBPay/delete',
      },
      queryParam: {
      },
      dataSource: [],
      loading: false,
      toggleSearchStatus: true,
      ipagination: {
        current: 1,
        pageSize: 30,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + this.$t('sum-total',{total:total})
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        onShowSizeChange: (current, pageSize) => this.handleSizeChange(current, pageSize),
      },
      typeList: [],
      rechargeTypeList: [],
      payTypeList: [],
      userList: [],
      title:this.$t('设置支付参数'),
      visible: false
    }
  },
  props: {
    accountId: String,
    required: true
  },
  created() {
    this.initDictConfig()
    this.loadData(1)
  },
  mounted() {
    this.$nextTick(() => {})
  },
  methods: {
    handleDelete(id) {
      var that = this
      if (!that.url.delete) {
        that.$message.error(that.$t('请设置url.delete属性'))
        return
      }
      deleteAction(that.url.delete, { id: id }).then((res) => {
        if (res.success) {
          //重新计算分页问题
          that.$message.success(res.message)
          that.loadData(1)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleAdd(){
      this.$refs.bbpayModalForm.edit()
      this.$refs.bbpayModalForm.title = this.$t('设置支付参数')
    },
    handleEdit(record){
      this.$refs.bbpayModalForm.edit(record)
      this.$refs.bbpayModalForm.title = this.$t('设置支付参数')
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = this.$t('导出文件')
      }
      downFile(this.url.exportXlsUrl, this.queryParam).then((data) => {
        if (!data) {
          this.$message.warning(this.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
    initDictConfig() {
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.type'}).then((res) => {
        if (res.success) {
          this.typeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.recharge_type'}).then((res) => {
        if (res.success) {
          this.rechargeTypeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.pay_type'}).then((res) => {
        if (res.success) {
          this.payTypeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'sys_user,realname,realname'}).then((res) => {
        if (res.success) {
          this.userList = res.result
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error(this.$t('请设置url.list属性'))
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.queryParam
      params.accountId = this.accountId
      if(params.createTime_begin === '' ||params.createTime_begin ===undefined){
        delete params.createTime_begin
      }
      if(params.createTime_end === '' ||params.createTime_end ===undefined){
        delete params.createTime_end
      }
      if(params.type === '' ||params.type ===undefined){
        delete params.type
      }
      if(params.createBy === '' ||params.createBy ===undefined){
        delete params.createBy
      }
      params.pageNo = this.ipagination.current;
      params.pageSize = this.ipagination.pageSize;
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    modalFormOk() {
      this.loadData(1)
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      console.log(pagination)
      this.ipagination = pagination;
      this.loadData();
    },    
    handleSizeChange(current, pageSize) {
      this.ipagination.pageSize = pageSize
    },
    
  },
}
</script>

<style scoped>
</style>